/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import BO.Controller;
import BO.User;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Wase
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    private Integer id;
    private String username;
    private String password;
    private String firstName;
    private String surName;
    private String address;
    private String zipcode;
    private String city;
    private String country;
    private String email;
    private boolean loggedIn;
    private Controller controller;
    private HttpSession session;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    
    private void setElementsOfUser(UserView user) {
        username = user.getUsername();
        firstName = user.getFirstName();
        surName = user.getSurName();
        address = user.getAddress();
        zipcode = user.getZipcode();
        city = user.getCity();
        country = user.getCountry();
        email = user.getEmail();
        id = user.getId();
    }
    
    public String validate(String username, String password) {
        UserView u = controller.validate(username, password);
        if(u != null){
            setElementsOfUser(u);
            try {
                session.setAttribute("user", u);
            } catch (NullPointerException e) {}
            loggedIn = true;
            password = "";
            return "successLogin";
        }
        loggedIn = false;
        return "failLogin";
    }
    
    public String getLoggedIn(){
        if(loggedIn)
            return "true";
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }
         return "false";
    }
    
    public String logInOrOut() {
        UserView user = null;
        try {
            user = (UserView) session.getAttribute("user");
        } catch (NullPointerException e) {}
        if (user != null) {
            loggedIn = false;
            session.invalidate();
        }
        return "logout";
    }
    
    public String isLoggedIn() {
        UserView user = null;
        try {
            user = (UserView) session.getAttribute("user");
        } catch (NullPointerException e) {}
        if (user != null) {
            return "LogOut";
        }
        return "LogIn";
    }
    
    /** Creates a new instance of LoginBean */
    public LoginBean() {
        controller = new Controller();
        session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }
}
